package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.OtherFollowType;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.BaseCase;
import cn.fintecher.pangolin.entity.elastic.OtherRecord;
import cn.fintecher.pangolin.entity.elastic.WorkOrderModel;
import cn.fintecher.pangolin.service.dataimp.elastic.BaseCaseElastic;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 15:21 2019/11/5
 */

@Service("confirmWorkOrderFuture")
public class ConfirmWorkOrderFuture {
    @Autowired
    private BaseCaseElastic baseCaseElastic;

    /**
     * 多线程确认
     */
    @Async
    public CompletableFuture<List<OtherRecord>> processWorkOrder(List<WorkOrderModel> list, UserModel userModel) {
        List<OtherRecord> otherRecords = new ArrayList<>();
        List<String> caseNumbers = new ArrayList<>();
        for (WorkOrderModel workOrderModel : list) {
            caseNumbers.add(workOrderModel.getCaseNumber());
        }
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.termsQuery("caseNumber.keyword", caseNumbers));
        Iterable<BaseCase> iterable = baseCaseElastic.search(builder);
        List<BaseCase> baseCases = IterableUtils.toList(iterable);
        for (WorkOrderModel workOrderModel : list) {
            OtherRecord otherRecord = new OtherRecord();
            otherRecord.setId(UUID.randomUUID().toString().replaceAll("-", "")); //主键ID
            otherRecord.setPrincipalId(workOrderModel.getPrincipalId()); //委托方ID
            otherRecord.setPrincipalName(workOrderModel.getPrincipalName()); //委托方名称
            for (BaseCase baseCase : baseCases) {
                if (workOrderModel.getCaseNumber().equals(baseCase.getCaseNumber())) {
                    otherRecord.setCaseId(baseCase.getId()); //案件ID
                    otherRecord.setCertificateNo(baseCase.getCertificateNo()); //证件号
                    break;
                }
            }
            otherRecord.setCaseNumber(workOrderModel.getCaseNumber()); //案件编号
            otherRecord.setFindTime(workOrderModel.getDate()); //查找时间
            otherRecord.setOtherFollowType(OtherFollowType.WORKORDER); //类型
            otherRecord.setRecord(workOrderModel.getContent()); //工单内容
            otherRecord.setBatchNumber(workOrderModel.getBatchNumber()); //批次号
            otherRecord.process(userModel.getId(), userModel.getRealName(), ZWDateUtil.getNowDateTime());
            otherRecords.add(otherRecord);
        }
        return CompletableFuture.completedFuture(otherRecords);
    }
}
